﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;

namespace ErJiBaoJiaDanBaoCun
{
    [Description("二级报价单保存服务插件，临时物料名称=名称 ，不允许保存")]
    [HotUpdate]
    public class ErJiBaoJiaDanBaoCun : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            // 添加预加载字段
            e.FieldKeys.Add("FBillNo");
            e.FieldKeys.Add("FMATERIALID");
            e.FieldKeys.Add("FMINGCHENG");


        }


        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            //循环所有的单据体
            foreach (DynamicObject item in e.DataEntitys)
            {
                //如果不为空
                if (item != null)
                {
                    var FBillNo = item["BillNo"].ToString();//编号

                    foreach (DynamicObject entity in e.DataEntitys)
                    {
                        DynamicObjectCollection entry = (DynamicObjectCollection)entity["SAL_QUOTATIONENTRY"];
                        foreach (DynamicObject HHentity in entry)
                        {
                            string FMATERIALID = HHentity["MaterialName"].ToString(); //物料名称
                            string FMINGCHENG = HHentity["FMINGCHENG"].ToString();  //名称

                            if (FMINGCHENG == "临时物料" && FMATERIALID == FMINGCHENG)
                            {

                                //this.View.ShowErrMessage("名称=物料名称，且为临时物料,不能保存");
                                //报错
                                string errMessage = "名称 = 物料名称，且为临时物料,不能保存";
                                throw new KDBusinessException("", errMessage);

                            }
                            else if(FMINGCHENG ==" ")
                            {
                                string errMessage = "名称不能为空";
                                throw new KDBusinessException("", errMessage);
                            }
                        }
                    }
                }
            }
        }



    }
}
